/**
 * Author(s): xuming.Wong <xuming.Wong@gmail.com>
 */
#include "ArchiveDialog.h"

#include <QtGui/QGridLayout>
#include <QtGui/QPushButton>
#include <QtGui/QLabel>
#include <QtGui/QFileDialog>
#include <QtGui/QMessageBox>

#include "ProjectManager.h"

namespace PQBuilder
{
	ArchiveDialog::ArchiveDialog(QWidget *parent)
	{
		QVBoxLayout* vBox=new QVBoxLayout(this);
		_createTab=new QWidget(this);
		_openTab=new QWidget(this);

		_tabWidget=new QTabWidget(this);
		_tabWidget->addTab(_createTab,QObject::tr("Create Archive"));
		_tabWidget->addTab(_openTab,QObject::tr("Open Archive"));

		createTabCreate();

		vBox->addWidget(_tabWidget);

		this->setLayout(vBox);
		this->setMaximumWidth(DIAGLOG_WIDTH);
		this->setMinimumWidth(DIAGLOG_WIDTH);
		this->setMaximumHeight(DIAGLOG_HEIGHT);
		this->setMinimumHeight(DIAGLOG_HEIGHT);
		this->setModal(true);
		this->setWindowTitle(QObject::tr("Archive"));
	}

	void ArchiveDialog::createTabCreate()
	{
		QGridLayout* grid=new QGridLayout(this);
		_createTab->setLayout(grid);
		
		QLabel* label=new QLabel(QObject::tr("Archive Name:"),this);
		label->setMaximumHeight(32);
		_pathEdit=new QLineEdit("",this);
		
		
		QPushButton* okButton=new QPushButton(QObject::tr("OK"),this);

		grid->addWidget(label,0,0);
		grid->addWidget(_pathEdit,1,0,1,2);
		grid->addWidget(okButton,2,1);
		grid->setColumnMinimumWidth(0,422);
		_createTab->setMaximumHeight(CREATE_TAB_HEIGHT);
		_createTab->setMinimumHeight(CREATE_TAB_HEIGHT);

		
		connect(okButton, SIGNAL(clicked()), this, SLOT(create()));
		
	}

	
	QStringList ArchiveDialog::getProjectPathes()
	{
		QStringList qStrList;
		
		std::vector<std::string> strList=GNgProjectManager.getProjectPathes();
		int size=strList.size();
		for(int i=0;i<size;i++){
			qStrList<<QString::fromStdString(strList[i]);
		}

		return qStrList;
	}

	void ArchiveDialog::createFileDialog()
	{
		QString dir = QFileDialog::getExistingDirectory(this, QObject::tr("Open Directory"),
                                                 QDir::currentPath(),
                                                 QFileDialog::ShowDirsOnly
                                                 | QFileDialog::DontResolveSymlinks);
		if(!dir.isEmpty()){
			_pathEdit->setText(dir);
		}
	}

	void ArchiveDialog::create()
	{
		QString name=_pathEdit->text();
		if(!name.isEmpty()){
			if(!GNgProjectManager.createAssetArchive(name.toStdString())){
				QMessageBox box;
				 box.setText(QObject::tr("Archive can't be created."));
				 box.exec();
				 return;
			}else{
				this->close();
				this->createArchiveSuccessed(name);
				return;
			}
		}else{
			QMessageBox box;
			 box.setText(QObject::tr("Archive name can only be simple text."));
			 box.exec();
			 return;
		}
		
	}

	void ArchiveDialog::loadProject()
	{
		
	}

	ArchiveDialog::~ArchiveDialog()
	{
	}
}
